package cn.tedu.baidashi.service.Impl;

import cn.tedu.baidashi.exception.ServiceException;
import cn.tedu.baidashi.mapper.OrderItemsMapper;
import cn.tedu.baidashi.mapper.OrdersMapper;
import cn.tedu.baidashi.model.Address;
import cn.tedu.baidashi.model.OrderItems;
import cn.tedu.baidashi.model.Orders;
import cn.tedu.baidashi.service.ICartService;
import cn.tedu.baidashi.service.IOrdersService;
import cn.tedu.baidashi.utils.BigDecimalUtil;
import cn.tedu.baidashi.utils.OrderConstant;
import cn.tedu.baidashi.vo.CartItemsVO;
import cn.tedu.baidashi.vo.CartVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2022-02-19
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderItemsMapper orderItemsMapper;
    @Autowired
    private ICartService cartService;
    public Long generateOrderNo(){
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String orderNo = dateformat.format(System.currentTimeMillis());
        return Long.parseLong(orderNo);
    }

    @Autowired
    private OrderAddressServiceImpl orderAddressService;

    @Override
    @Transactional
    public String createOrder(Integer shippingId, CartVO cartVO) {
        List<CartItemsVO> cartItemsVOLists = cartVO.getCartItemsVOList();
        if(cartItemsVOLists.size()==0){
            throw new ServiceException("购物车是空的,创建订单失败");
        }
        Address address = orderAddressService.getAddressByUserNameAndAddressId(shippingId,"江颖");
        BigDecimal orderTotal = new BigDecimal(cartVO.getOrderTotal());
        Long orderNo = generateOrderNo();
        Orders order = new Orders();
        order.setOrderNo(orderNo);
        order.setUserId(1);
        order.setOrderTotal(orderTotal);
        order.setPaymentType(1);    //支付宝
        order.setReceiverName(address.getCustomerName());
        order.setReceiverProvince(address.getProvinceName());
        order.setReceiverCity(address.getCityName());
        order.setReceiverDistrict(address.getZoneName());
        order.setReceiverStreet(address.getStreet());
        order.setReceiverPhone(address.getPhone());
        order.setStatus(OrderConstant.ORDER_WAY_PAY); //未付款
        order.setCreateTime(LocalDateTime.now());
        System.out.println(order);
        int num = ordersMapper.insert(order);
        if(num!=1){
            throw new ServiceException("创建订单失败");
        }
        for (CartItemsVO cartItemsVOList : cartItemsVOLists) {
            OrderItems orderItems = new OrderItems();
            orderItems.setOrderNo(orderNo);
            orderItems.setItemsId(cartItemsVOList.getItemsId());
            orderItems.setItemsName(cartItemsVOList.getItemsName());
            orderItems.setItemsPrice(new BigDecimal(cartItemsVOList.getUtilPrice()));
            orderItems.setQuantity(cartItemsVOList.getItemsCount());
            orderItems.setItemsImage(cartItemsVOList.getImage());
            orderItems.setFinalPrice(BigDecimalUtil.multiply(new BigDecimal(cartItemsVOList.getUtilPrice()).doubleValue(),new BigDecimal(cartItemsVOList.getItemsCount()).doubleValue()));
            num = orderItemsMapper.insert(orderItems);
            if(num!=1){
                throw new ServiceException("数据库异常");
            }
        }
        return orderNo.toString();
    }


    // 根据userId和orderNo查询订单
    @Override
    public Orders getOrderDetail(Integer userId, Long orderNo) {
        Orders order = ordersMapper.getOrderDetailByUserIdAndOrderNo(userId, orderNo);
        if(order==null){
            throw new ServiceException("用户不存在该订单");
        }
        return order;
    }

//    @Override
//    public PageInfo<Orders> getOrders(Integer pageNum,Integer pageSize) {
//        PageHelper.startPage(pageNum,pageSize);
//        List<Orders> ordersList=ordersMapper.getOrders();
//        return new PageInfo<>(ordersList);
//    }


}
